//
// TwitterBackend.cs
//
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
//

using System;
using System.Collections.Generic;

using Dashboard;
using Dashboard.Util;
using Twitter;

namespace Dashboard.Engine.Backends {

	public class TwitterBackend : Backend, IComponent {

		public void Initialize () 
		{
			Engine.MessageBroker.Subscribe ("/event/*", typeof (ClueMessage), OnMessage);
		}

		public void Dispose ()
		{
			Engine.MessageBroker.Unsubscribe (OnMessage);
		}

		private void OnMessage (Message message)
		{
			Engine.Log.Debug ("Processing message");
			
			MatchMessage matches_message = new MatchMessage ();

			foreach (TwitterStatus status in TwitterClient.PublicTimeline ()) {
				// FIXME: What!? 
				Match match = new Match ("db:tweet");
				match.Uri = new Uri (String.Format ("http://twitter.com/{0}", status.User.Name));
				match.Properties.Add (new Property ("dc:title", status.Text));
				match.Properties.Add (new Property ("dc:creator", status.User.Name));
				
				matches_message.Matches.Add (match);

				break; // FIXME: Were just testing this, one tweet per clue message
			}

			Engine.MessageBroker.Send (matches_message);
		}

		public string Name {
			get { return "Twitter"; }
		}

		public string Description {
			get { return ""; }
		}
	}
}